package com.brotherly.framework.log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.os.Environment;
import android.util.Log;

import com.brotherly.framework.service.IService;
import com.brotherly.framework.storage.SimpleStorage;
import com.brotherly.framework.storage.SimpleStorageService;
import com.brotherly.framework.transaction.TransactionServiceHost;

public class LogService implements IService
{
    public static final DateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:s", Locale.US);

    public static final String description = "com.brotherly.framework.log.LOG";

    public static final String Log_Name = "ValentineClock.log";

    public static final String LogStorage = "com.brotherly.framework.log";

    public static final String Option_Log_Level = "Log_Level";

    public static final String ValentineClock_Name = "ValentineClock";

    protected int level = 0;

    protected PrintStream printer = null;

    public LogService()
    {
        File logfile = new File(getLogFilename());

        try
        {
            logfile.getParentFile().mkdirs();

            if (logfile.exists())
                logfile.delete();

            logfile.createNewFile();

            FileOutputStream output = new FileOutputStream(logfile, true);

            printer = new PrintStream(output);

            getLevel();
        }
        catch (Exception e)
        {}
    }

    public int getLevel()
    {
        SimpleStorageService stroage = TransactionServiceHost.getInstance().getStorageServ();

        SimpleStorage storage = stroage.openStorage(LogStorage);
        String value = storage.getWithDefault(Option_Log_Level, "0");

        storage.close();

        level = Integer.parseInt(value);

        return level;
    }

    protected String getLogFilename()
    {
        String root = "";

        if (!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        {
            root = TransactionServiceHost.getInstance().getApplicationInfo().dataDir;
        } else
        {
            root = Environment.getExternalStorageDirectory().getAbsolutePath();
        }

        String filename = String.format("%s/%s/%s", root, ValentineClock_Name, Log_Name);

        return filename;
    }

    public String getLogFileName()
    {
        return getLogFilename();
    }

    @Override
    public void init()
    {}

    public void log(int level, String name, String message)
    {
        if (level >= this.level)
        {
            printer.println(String.format("%s %s: %s", DateFormat.format(new Date()), name, message));
            Log.i(name, message);

            printer.flush();
        }
    }

    public void setLevel(int level)
    {
        SimpleStorageService stroage = TransactionServiceHost.getInstance().getStorageServ();

        SimpleStorage storage = stroage.openStorage(LogStorage);

        storage.put(Option_Log_Level, String.valueOf(level));
        storage.commit();

        storage.close();
    }
}
